rm(list = ls(all = TRUE))
#library(quanteda)

# DATA AND PATHS
# --------------
inFile <- "./generated_data/1-corpus_and_wfm.RData"
outFile <- "./generated_data/2-estimated_positions.RData"

load(inFile)

# temp data frame that will hold the results
tmp <- data.frame()

for (i in sort(unique(data$debate.year))) {

    # code reference docs: PM (ref1), FM (ref2)
    # (use Tanaiste in 2000 because Taoiseach (Bertie Ahern) didn't speak)      
    ref1 <- which(rownames(wfm.list[[as.character(i)]]) %in% data$memberID[data$pm==1 & data$debate.year==i])

    ref2 <- which(rownames(wfm.list[[as.character(i)]]) %in% data$memberID[data$fm==1 & data$debate.year==i])    
    
    refscores <- rep(NA,nrow(wfm.list[[as.character(i)]]))
    refscores[ref1] <- 1
    refscores[ref2] <- -1
    
    ws <- textmodel_wordscores(wfm.list[[as.character(i)]], refscores, smooth=1)

    ws.cab <- predict(ws)
    ws.cab.rescaled <- predict(ws, rescaling="mv")
   
    tmp <- rbind(tmp,data.frame(debate.year=i,memberID=rownames(wfm.list[[as.character(i)]]),ws.cab,ws.cab.rescaled,refscores))
    
}

# merge with data object
data <- merge(data,tmp,by=c("debate.year","memberID"),all.x=TRUE)

# save image
save(data,wfm.list,file=outFile)
